Skip to content

fix(libc): preserve expf rounding and exception behavior - #130

Open
Thimo188 wants to merge 28 commits into
mainfrom
upstream-pr/10113
Open

Thimo188 wants to merge 28 commits into
mainfrom
upstream-pr/10113

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#10113, originally opened by JJenkx.

Source commit: 9a339843cfb400226067217d2c7058846705bde0. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

expf now preserves the scalar bundled-libc algorithm's results and floating-point side effects across caller rounding modes. It uses the existing msun-derived reduction and polynomial, with the native range branches, evaluation order and fixed binary32 constants.

  • Overflow calls _Feraise(8) and returns positive infinity. The explicit underflow branch calls _Feraise(16) before the final multiplication; the general subnormal path leaves errno unchanged.
  • The native argument-1 literal returns without adding inexact. Small arguments retain the huge + x inexact trigger.
  • Arithmetic follows the native 1 + (x - ratio) order. Exact hexadecimal float constants avoid runtime double-to-float conversion under directed rounding.
  • MathExpf.cpp retains the existing Sun/msun licence and attribution, with source-scoped -frounding-math and -ffp-contract=off.

The log1pf prerequisite from boykopovar#10007 and its authored history remain intentionally retained. This updates the existing PR; no duplicate expf implementation PR is created.

Tested

Current-head Linux validation (2026-11-27): f876efc0ef1c529370c6304149e3adb8443e27f4 merges main f73d791b3df4dcaa773297a93f5ae2c062ee95a3 and retains main's scalar and fused logf paths alongside this PR's dedicated MathExpf.cpp. All six targeted Linux x86-64 Release CTests passed with zero skips. The guest_expf_environment suite passed all 1,128 checks, and the dedicated source retained -frounding-math -ffp-contract=off. Exact-head Linux report.

Historical CMake maintenance (2026-11-26): head 996aaa42e5f8c7d72bf09031e317b2c9e7228bcf merges main 79e4f35199fa93b44e4689f391f603dec5b702cb. The maintenance commit changes only CMake placement: the expf source/flag blocks avoid the gamma insertion points, and guest_expf_environment moves into an independent test fragment with its original BUILD_TESTING guard. MathExpf.cpp and GuestExpfEnvironment.cpp remain byte-identical to public head 86fc4fc9c2f0.

A fresh Linux x86-64 / GCC 16.2.1 / Release Ninja build of raw libc/kernel and four focused executables passes. guest_expf_environment, guest_math, guest_math_elementary and guest_floating_point pass, 4/4, zero skips. The CTest backtrace confirms registration from the new fragment; Ninja retains -frounding-math and -ffp-contract=off; actual raw-provider loading and unchanged input hashes are verified. Conventions, debt-fold and whitespace checks pass. Full Git pair merges with boykopovar#10226 (ca8a86166e6f), boykopovar#24306 (0fbd95fdc9d6) and boykopovar#24360 (53137577143a) are clean; the gamma pairs conflicted in CMake before this maintenance. These are merge checks, not new gamma runtime tests.

Maintenance patch, build/test helpers, compiler flags, registration/loader receipts, logs and JUnit — anonymous download and all 20 payload hashes verified; SHA-256 8bd6922de236eda39482f5021367095aca6bfebed52f2d09464650455e5c2ce8. This maintenance validation did not rerun the full patched SDK, Windows, original-module differential, console, title/GPU or performance checks. Earlier mathematical validation remains pinned to the revisions below.

Earlier refresh: merged upstream 786bc3afa1c874f98ec2990eb58179a99eeb425c as 86fc4fc9c2f04da5210c6cf23a69aa2f4eed05ce. The CMake conflict keeps both this PR's guest_expf_environment registration and main's guest_lgamma_poles registration, including their separate Windows environment blocks. The production MathExpf.cpp, golden fixture and debt fragment are byte-identical to the corrected prior head 61549d86.

At that earlier pin on Linux 7.3.5-arch1-1, the complete patched/raw SDK and relinker build pass. All five focused CPU groups pass: guest_math, guest_math_exact, guest_dinkumware_internals, guest_floating_point, guest_expf_environment. Source, configuration and submodules were restored. Current-main patch, build provenance, frozen SDK hashes and verbose test logs, SHA-256 811511ebd9c391badddb43bf75489f38728e09bbf92f8ebe24bf11134a634ee8; downloaded again and verified all 19 payload hashes plus the manifest. Hosted CI was pending when that refreshed-pin report was written. The Windows owner also reports the earlier public head 86fc4fc9c2f0 merged onto main 87b7ef3891 as c553316ef1: guest_expf_environment, guest_lgamma_poles and guest_math all pass in each of three runs on Windows 11 Enterprise 22621 / WinLibs GCC 15.2 UCRT Release. This is a three-group Windows check, not a claim that the whole Windows suite ran. Owner report.

The original differential and trap measurements below remain historical evidence at their named pins.

Arch Linux x86-64, GCC 16.2.1, Release. Main 7fa48cd1c6cda62b4ebe86575085199a33e08c1d; old public boykopovar#10113 ea1052637d616b6ec43ed9fd0ec4ed90f971f2a6; corrected head 61549d86c2a61edbd005d9b441748b3d9bd906aa. Comparisons use frozen private merges of the public branches onto that main. The old-PR composition and the corrected head's pre-fix parent have identical source trees.

Full SDK libs, relinker and four existing CPU groups built and passed on main, main plus old boykopovar#10113, and main plus boykopovar#21714. The correction also passes the new guest_expf_environment test: 1,128 result/flag/errno/control checks, including a pending divide-by-zero flag. All five groups pass. The exact same new executable fails against both main and old boykopovar#10113 raw test libraries, then passes with the correction; loader paths are verified and loader failures are excluded.

Actual patched SDK versus the original converted PPSA21564 libc export (ab860676108827e0983eefbde3a711b5ce8e20f0b24b492498b6e04cb23c6130): 6,906 inputs × three x87 precisions × four rounding modes × four DAZ/FTZ states × two pending states = 662,976 comparisons per bank. Both function owners are checked by device/inode, reference outcomes are identical across providers, and banks/modules/probe hashes remain unchanged.

Provider Result differences Raw exception-flag differences Errno differences
Main 121,704 924 3,840
Main + old boykopovar#10113 83,364 2,124 0
Main + boykopovar#21714 121,704 12,912 15,576
Corrected boykopovar#10113 0 0 0

Caller control differences: zero for every bank. The SDK's __error aliases host errno on this Linux machine, so the two logged errno columns are one channel, not independent measurements.

A separate Linux child-process comparison enables exception traps in both x87 and SSE: 144 reference/SDK pairs, checking first-trap signal, flags, errno, result and controls. Old boykopovar#10113 has 96 mismatches; the correction has zero. Original-module dependencies, including _Feraise, still come from each SDK, so this does not independently establish console/helper conformance.

Complete evidence, source patches and reproducers — SHA-256 eb013afbaf76b1a234a17402ee3c0afc5befd14cf1543813613bdcbe88903bf1, 166 payloads; downloaded and every payload verified. Original binaries, original assembly, game assets, SDK binaries and caches are excluded.

Conventions pass; the merge-tree check against current main is clean. The official overlap scan and pinned manual comparisons are included. boykopovar#10226 is the dependent erff/erfcf child; boykopovar#21714 is a tested alternative, not a dependency; boykopovar#19434 forwards a separate library surface; boykopovar#24322 splits source files. The gamma PRs share independent additive CMake registrations. All fifteen fail-closed primary patches match previously reviewed pins, and the two additional shared test hunks concern nextafter/nexttoward and double lgamma only.

These are sampled Linux CPU measurements, not a new exhaustive all-float result. Prior Windows/macOS reports on the old public head remain historical. The separate Windows owner report above covers three focused groups on the refreshed head composed with its named main; it does not reproduce the original-module differential or trap comparisons. No new macOS qualification is available. FMA-contracted title variants can retain different last bits, as documented in debt. No console execution, game unblock, GPU, loading-time or FPS improvement is claimed.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: fix(libc): follow msun for log1pf boykopovar/AnyPS5#10007
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Improved expf results across normal and very small inputs, including overflow, underflow, and subnormal cases.
    • Improved expf behavior across rounding modes and floating-point exception conditions.
    • Improved log1pf results for finite inputs within its valid range, while retaining handling for values outside that range.
  • Documentation
    • Added notes on updated math behavior and reported comparison results, including coverage and areas that have not yet been verified.

MarcZX10 and others added 28 commits October 26, 2026 22:17
log1pf now runs the whole of FreeBSD's s_log1pf.c for finite inputs above -1 instead of mapping negative inputs to -log1p(-x/(1+x)). Compared with the pinned source over every finite float above -1 it has no differing result bit; the old mapping differed on about 82 million inputs, the first at 0xb3000001.
…duction boundaries

The expected bits come from the pinned s_log1pf.c.
(cherry picked from commit 87f635c)
(cherry picked from commit 134201f)
log1pf now runs the whole of FreeBSD's s_log1pf.c for finite inputs above -1 instead of mapping negative inputs to -log1p(-x/(1+x)). Compared with the pinned source over every finite float above -1 it has no differing result bit; the old mapping differed on about 82 million inputs, the first at 0xb3000001.

(cherry picked from commit ae53b71)
…duction boundaries

The expected bits come from the pinned s_log1pf.c.

(cherry picked from commit 3fcdc09)
expf follows lib/msun/src/e_expf.c and log1pf follows
lib/msun/src/s_log1pf.c, both at release/11.4.0. Add their source
line, the float-conversion credit and the Sun notice as those files
carry them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main's boykopovar#21714 set expf's ERANGE in the Math.cpp expf this branch replaces.
The branch's msun expf in MathExpf.cpp keeps the console's errno (overflow
and the underflows the console reports; guest_math pins those bits), so the
Math.cpp definition stays removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 133 core/libs/prx/libc/src/MathExpf.cpp
core/libs/prx/libc/tests/cmake/guest_expf_environment.cmake
core/libs/tests/GuestExpfEnvironment.cpp
docs/dev/debt/changes/refresh-expf-log1pf-msun-20261117.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants